package com.sunlg.smarthome.ui.curtain;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.CurtainContract;
import com.sunlg.smarthome.contract.presenter.CurtainListPresenter;
import com.sunlg.smarthome.data.entity.Curtain;
import com.sunlg.smarthome.ui.wedget.RecyclerViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CurtainListActivity extends BaseActivity
        implements CurtainContract.CurtainListView, AddCurtainDialog.OnEditTextInputListener{

    @BindView(R.id.rv_curtain_list)
    RecyclerView rvCurtainList;
    @BindView(R.id.tv_none_device)
    TextView tvNoneDevice;

    private List<Curtain> mCurtainList;
    private CurtainAdapter mCurtainAdapter;
    private CurtainContract.CurtainListPresenter presenter;

    private BroadcastReceiver mCurtainChangedBr;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_curtain_list;
    }

    @Override
    protected void init() {
        presenter = new CurtainListPresenter();
        presenter.attachView(this);

        mCurtainList = new ArrayList<>();
        mCurtainAdapter = new CurtainAdapter(this, mCurtainList);
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rvCurtainList.setLayoutManager(layoutManager);
        rvCurtainList.addItemDecoration(new RecyclerViewDivider(this, LinearLayoutManager.VERTICAL));
        rvCurtainList.setAdapter(mCurtainAdapter);

        presenter.loadAll();

        mCurtainChangedBr = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //改变状态
                Bundle bundle = intent.getExtras();
                int level = bundle.getInt("level");
                int pos = bundle.getInt("pos");
                Curtain curtain = mCurtainList.remove(pos);
                curtain.setStatus(level);
                mCurtainList.add(pos, curtain);

                //lightAdapter.notifyDataSetChanged();
                mCurtainAdapter.notifyItemChanged(pos);
            }
        };
        IntentFilter intentFilter = new IntentFilter(C.Action.CURTAIN_STATUS_CHANGED);
        registerReceiver(mCurtainChangedBr, intentFilter);
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("窗帘控制");
        actionBar.setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.action_add){
            AddCurtainDialog dialog = new AddCurtainDialog(this);
            dialog.setOnEditTextInputListener(this);
            dialog.show();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onEditTextInput(Integer no, String inputText) {
        Curtain curtain = new Curtain();
        curtain.setDevice(no);
        curtain.setDescription(inputText);

        presenter.addCurtain(curtain);
    }

    @Override
    public void showNoneCurtainText() {
        if(tvNoneDevice.getVisibility() != View.VISIBLE){
            tvNoneDevice.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideNoneCurtainText() {
        if(tvNoneDevice.getVisibility() == View.VISIBLE){
            tvNoneDevice.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void showCurtainList(List<Curtain> curtainList) {
        mCurtainList.clear();
        mCurtainList.addAll(curtainList);
        mCurtainAdapter.notifyDataSetChanged();
    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.dettachView();
        unregisterReceiver(mCurtainChangedBr);
    }
}
